package cn.yunsenlin.formula.cloud.auth;

import cn.yunsenlin.formula.cloud.interfaces.AuthService;
import cn.yunsenlin.formula.cloud.responsibility.ResponseResponsibilityHandler;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 检测session中的用户信息是否合法，继承了责任链责任机制
 */
@Service
public class AuthRRH extends ResponseResponsibilityHandler {
    private final AuthService authService;

    public AuthRRH(AuthService authService) {
        this.authService = authService;
    }

    @Override
    protected Object doHandler(HttpServletResponse response, Object object) {
        if(!authService.sessionHasValidAuth((HttpSession) object)){
            response.setStatus(403);
        }
        return null;
    }

    @Override
    protected boolean continueAble(HttpServletResponse response, Object object) {
        return authService.sessionHasValidAuth((HttpSession) object);
    }
}
